package com.yqg.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.manage.entity.RoleMenu;
import com.yqg.manage.mapper.RoleMapper;
import com.yqg.manage.mapper.RoleMenuMapper;
import com.yqg.manage.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * RoleMenuServiceImpl 服务实现类
 *
 * @author zxf
 * @since 2025-09-16
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Long> getDataByUnitId(Long unitId) {
        return roleMenuMapper.getDataByUnitId(unitId);
    }

    @Override
    public List<Long> getDataByRoleIdAndUnitId(Long roleId, Long unitId) {
        return roleMenuMapper.getDataByRoleIdAndUnitId(roleId, unitId);
    }

    @Override
    public boolean saveOrUpdateRoleMenu(List<Long> menuIds, Long unitId, Long roleId) {
        // 处理null情况，menuIds为空表示要清除所有关联
        if (unitId == null || roleId == null) {
            return false; // unitId和roleId都不能为空
        }
        if (menuIds == null) {
            menuIds = Collections.emptyList();
        }

        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getUnitId, unitId)
                .eq(RoleMenu::getRoleId, roleId);
        List<RoleMenu> oldDataList = list(queryWrapper);
        Set<Long> oldDataIds = oldDataList.stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toSet());

        // 需要新增的菜单
        Set<Long> addMenuIds = menuIds.stream()
                .filter(menuId -> !oldDataIds.contains(menuId))
                .collect(Collectors.toSet());

        // 需要删除的菜单ID
        List<Long> finalMenuIds = menuIds;
        List<Long> toDeleteIds = oldDataList.stream()
                .filter(roleMenu -> !finalMenuIds.contains(roleMenu.getMenuId()))
                .map(RoleMenu::getId)
                .collect(Collectors.toList());

        // 执行删除操作
        if (!toDeleteIds.isEmpty()) {
            this.removeByIds(toDeleteIds);
        }

        // 执行新增操作
        List<RoleMenu> addDataBaseList = new ArrayList<>();
        addMenuIds.forEach(addId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(addId);
            roleMenu.setUnitId(unitId);
            roleMenu.setRoleId(roleId);
            addDataBaseList.add(roleMenu);
        });

        return addDataBaseList.isEmpty() || this.saveBatch(addDataBaseList);
    }

    @Override
    public Boolean removeMenuIdsByUnitId(List<Long> toDeleteRoleMenuIds, Long unitId) {
        roleMenuMapper.removeMenuIdsByUnitId(toDeleteRoleMenuIds,unitId);
        return true;
    }
}
